#include "stdafx.h"

#pragma warning(disable:4996)


CString GetKeyStringValue(CString line, CString key)
{
	CString s;
	int pos1,pos2;
	int n=key.GetLength()+1;
	if(line!="")
	{
		pos1=line.Find(key);
		if(pos1<0) return _T("");
		pos2=line.Find(_T(";"), pos1);
		if(pos2<0) return _T("");
		s=line.Mid(pos1+n, pos2-pos1-n);
	}

	return s;
}

CString GetEqualStringValue(CString line, CString key)
{
	CString s;
	int pos1;
	int n=key.GetLength()+1;
	if(line!="")
	{
		pos1=line.Find(key);
		if(pos1<0) return _T("");
		s=line.Right(line.GetLength() - pos1 - n);
	}

	return s;
}

CString MemoryToString(void* pMem, size_t nLen)
{
	CString str;
	str.Empty();
	CString s;
	BYTE* p=(BYTE*)pMem;
	for(size_t n=0; n<nLen; n++,p++)
	{
		s.Format(_T("%02X"), *p);
		str+=s;
	}
	return str;
}

CString	TimeToString(const SYSTEMTIME* time)
{
	CString s;
	s.Format(_T("%04d-%02d-%02d %02d:%02d:%02d"), time->wYear, time->wMonth, time->wDay, time->wHour, time->wMinute, time->wSecond);
	return s;
}

void StringToTime(SYSTEMTIME* time, LPCTSTR str)
{
	_stscanf(str, _T("%04d-%02d-%02d %02d:%02d:%02d"), &time->wYear, &time->wMonth, &time->wDay, &time->wHour, &time->wMinute, &time->wSecond);
}

void StringToRect(RECT* pRect, LPCTSTR pString)
{
	CString value=pString;
	CString s;
	int n;
	n=value.Find(_T(","));
	s = value.Left(n);
	value = value.Right(value.GetLength() - n - 1);
	pRect->left = _ttoi(s);
	n=value.Find(_T(","));
	s = value.Left(value.Find(_T(",")));
	value = value.Right(value.GetLength() - n - 1);
	pRect->top = _ttoi(s);
	n=value.Find(_T(","));
	s = value.Left(value.Find(_T(",")));
	value = value.Right(value.GetLength() - n - 1);
	pRect->right = _ttoi(s);
	pRect->bottom = _ttoi(value);
}

void StringToPoint(POINT* pPoint, LPCTSTR pString)
{
	CString value=pString;
	CString s;
	int n;
	n=value.Find(_T(","));
	s = value.Left(n);
	value = value.Right(value.GetLength() - n - 1);
	pPoint->x = _ttoi(s);
	pPoint->y = _ttoi(value);
}

void StringToSize(SIZE* pSize, LPCTSTR pString)
{
	CString value=pString;
	CString s;
	int n;
	n=value.Find(_T(","));
	s = value.Left(n);
	value = value.Right(value.GetLength() - n - 1);
	pSize->cx = _ttoi(s);
	pSize->cy = _ttoi(value);
}

void StringToRectWH(RECT* pRect, LPCTSTR pString)
{
	int width,height;
	CString value=pString;
	CString s;
	int n;
	n=value.Find(_T(","));
	s = value.Left(n);
	value = value.Right(value.GetLength() - n - 1);
	pRect->left = _ttoi(s);
	n=value.Find(_T(","));
	s = value.Left(value.Find(_T(",")));
	value = value.Right(value.GetLength() - n - 1);
	pRect->top = _ttoi(s);
	n=value.Find(_T(","));
	s = value.Left(value.Find(_T(",")));
	value = value.Right(value.GetLength() - n - 1);
	width = _ttoi(s);
	height = _ttoi(value);
	pRect->right=pRect->left + width;
	pRect->bottom=pRect->top + height;
}

CString HexToString( LPCWSTR szHex )
{
	CString str;
	WCHAR Char;
	WCHAR TempString[5]=_T("");
	size_t n=wcslen(szHex);
	for(size_t i=0; i<n; i+=4)
	{
		TempString[2]=szHex[i+0];
		TempString[3]=szHex[i+1];
		TempString[0]=szHex[i+2];
		TempString[1]=szHex[i+3];
		Char=(WCHAR)wcstol(TempString,NULL,16);
		str+=Char;
	}
	return str;
}
